<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2019-03-26
 */

namespace app\admin\controller;

use think\Page;
use think\Db;
use think\Config;
use app\admin\logic\ShopLogic;
use app\admin\logic\ProductSpecLogic; // 用于产品规格逻辑功能处理
use app\user\model\Pay as PayModel;  //用于虚拟网盘商品付款后自动走流程处理

class Shop extends Base
{
    private $UsersConfigData = [];

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->users_db = Db::name('users');                   // 会员信息表
        $this->shop_order_db = Db::name('shop_order');              // 订单主表
        $this->shop_order_details_db = Db::name('shop_order_details');      // 订单明细表
        $this->shop_address_db = Db::name('shop_address');            // 收货地址表
        $this->shop_express_db = Db::name('shop_express');            // 物流名字表
        $this->shop_order_log_db = Db::name('shop_order_log');          // 订单操作表
        $this->shipping_template_db = Db::name('shop_shipping_template');  // 运费模板表
        $this->product_spec_preset_db = Db::name('product_spec_preset');    // 产品规格预设表

        // 会员中心配置信息
        $this->UsersConfigData = getUsersConfigData('all');
        $this->assign('userConfig', $this->UsersConfigData);

        // 用于产品规格逻辑功能处理
        $this->ProductSpecLogic = new ProductSpecLogic;

        // 模型是否开启
        $channeltype_row = \think\Cache::get('extra_global_channeltype');
        $this->assign('channeltype_row', $channeltype_row);

        // 订单业务层
        $this->ShopLogic = new ShopLogic;
        // 商城订单处理
        // $this->ShopLogic->eyou_adminShopOrderHandle();
        // 过期订单删除处理
        // $this->ShopLogic->eyou_overdueOrderHandle();
    }

    public function home()
    {
        $url = url('Statistics/index', [], true, true);
        $url = preg_replace('/^http(s?)/i', $this->request->scheme(), $url);
        $this->redirect($url);
        exit;
    }

    /**
     * 基础信息
     */
    public function conf()
    {
        // 文件上传最大限制
        $maxFileupload = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 0;
        if (0 !== $maxFileupload) {
            $max_filesize = unformat_bytes($maxFileupload);
            $max_filesize = $max_filesize / 1024 / 1024; // 单位是MB的大小
        } else {
            $max_filesize = 500;
        }
        $max_sizeunit = 'MB';
        $maxFileupload = $max_filesize.$max_sizeunit;

        if (IS_POST) {
            $post = input('post.');
            if (!empty($post)) {
                $inc_type_arr = ['web', 'basic', 'order', 'shop', 'goods'];
                foreach ($inc_type_arr as $v) {
                    $data = $post[$v];
                    if (in_array($v, ['web', 'basic'])) {
                        if ($v == 'web') {
                            // 网站根网址
                            $web_basehost = request()->host(true);
                            if (!empty($web_basehost)) {
                                $web_basehost = $this->request->scheme().'://'.$web_basehost;
                            }
                            $data['web_basehost'] = $web_basehost;
                        }
                        else if ($v == 'basic') {
                            $data['file_size'] = intval($data['file_size']);
                            if (0 < $max_filesize && $max_filesize < $data['file_size']) {
                                $this->error("附件上传大小超过空间的最大限制".$maxFileupload);
                            }

                            // 允许上传图片类型
                            $image_ext = config('global.image_ext');
                            $image_ext_arr = explode(',', $image_ext);
                            $image_type = explode('|', $data['image_type']);
                            foreach ($image_type as $_k => $_v) {
                                $_v = trim($_v);
                                if (!in_array($_v, $image_ext_arr) || empty($_v)) {
                                    unset($image_type[$_k]);
                                }
                            }
                            $data['image_type'] = implode('|', $image_type);

                            // 允许上传软件类型，类型太多无法进行白名单处理
                            $file_type = explode('|', $data['file_type']);
                            foreach ($file_type as $_k => $_v) {
                                $_v = trim($_v);
                                if (preg_match('/^(php|asp|jsp|perl|cgi|asa|pht|phtml)/i', $_v) || empty($_v)) {
                                    unset($file_type[$_k]);
                                }
                            }
                            $data['file_type'] = implode('|', $file_type);

                            // 允许多媒体类型
                            $media_ext = config('global.media_ext');
                            $media_ext_arr = explode(',', $media_ext);
                            $media_type = explode('|', $data['media_type']);
                            foreach ($media_type as $_k => $_v) {
                                $_v = trim($_v);
                                if (!in_array($_v, $media_ext_arr) || empty($_v)) {
                                    unset($media_type[$_k]);
                                }
                            }
                            $data['media_type'] = implode('|', $media_type);
                            /*end*/
                        }

                        tpCache($v, $data);

                        if ($v == 'web') {
                            write_global_params(); // 写入全局内置参数
                        }
                    }
                    else if (in_array($v, ['order', 'shop', 'goods'])) {
                        getUsersConfigData($v, $data);
                    }
                }
                $this->success('操作成功', null, url('Shop/conf'));
            }
            $this->error('操作失败', null, url('Shop/conf'));

        }

        $this->assign('max_filesize', $max_filesize);
        $this->assign('max_sizeunit', $max_sizeunit);

        /*代理贴牌功能限制-s*/
        $upgrade = true;
        if (function_exists('checkAuthRule')) {
            //系统升级
            $upgrade = checkAuthRule('upgrade');
        }
        $this->assign('upgrade', $upgrade);
        /*代理贴牌功能限制-e*/
        
        return $this->fetch('conf');
    }

    /**
     *  订单列表
     */
    public function index()
    {
        // 初始化数组和条件
        $where = [
            'merchant_id' => 0,
        ];

        // 订单号查询
        $order_code = input('order_code/s');
        if (!empty($order_code)) $where['a.order_code'] = ['LIKE', "%{$order_code}%"];

        // 支付方式查询
        $pay_name = input('pay_name/s');
        if (!empty($pay_name)) $where['a.pay_name'] = $pay_name;
        $this->assign('pay_name', $pay_name);

        // 订单下单终端查询
        $order_terminal = input('order_terminal/d');
        if (!empty($order_terminal)) $where['a.order_terminal'] = $order_terminal;
        $this->assign('order_terminal', $order_terminal);

        // 商品类型查询
        $contains_virtual = input('contains_virtual/d');
        if (!empty($contains_virtual)) $where['a.contains_virtual'] = $contains_virtual;
        $this->assign('contains_virtual', $contains_virtual);

        // 下单时间查询
        $add_time = input('param.add_time/s');
        if (!empty($add_time)) {
            $add_time = explode('~', $add_time);
            $start = strtotime(rtrim($add_time[0]));
            $finish = strtotime(rtrim($add_time[1]).' 23:59:59');
            $where['a.add_time'] = ['between', "$start, $finish"];
        }

        //时间检索条件
        // $begin = strtotime(input('param.add_time_begin/s'));
        // $end = input('param.add_time_end/s');
        // !empty($end) && $end .= ' 23:59:59';
        // $end = strtotime($end);
        // // 时间检索
        // if ($begin > 0 && $end > 0) {
        //     $where['a.add_time'] = array('between', "$begin, $end");
        // } else if ($begin > 0) {
        //     $where['a.add_time'] = array('egt', $begin);
        // } else if ($end > 0) {
        //     $where['a.add_time'] = array('elt', $end);
        // }

        // 订单状态查询
        $order_status = input('order_status/s');
        if (!empty($order_status)) $where['a.order_status'] = (9 == $order_status) ? 0 : $order_status;

        // 分页查询
        $count = $this->shop_order_db->alias('a')->where($where)->count('order_id');
        $pageObj = new Page($count, config('paginate.list_rows'));

        // 订单主表数据查询
        $list = $this->shop_order_db->alias('a')
            ->field('a.*, b.username as u_username, b.nickname as u_nickname, b.mobile as u_mobile')
            ->where($where)
            ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
            ->order('a.order_id desc')
            ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
            ->select();
        if (empty($list) && !empty($order_code)) {
            // 通过商品名称查询订单号
            $where_1['product_name'] = ['LIKE', "%{$order_code}%"];
            $order_ids = $this->shop_order_details_db->where($where_1)->group('order_id')->column('order_id');
            // 重新查询订单主表
            unset($where['a.order_code']);
            $where['a.order_id'] = ['IN', $order_ids];
            // 分页查询
            $count = $this->shop_order_db->alias('a')->where($where)->count('order_id');
            $pageObj = new Page($count, config('paginate.list_rows'));
            // 订单主表数据查询
            $list = $this->shop_order_db->alias('a')
                ->field('a.*, b.username as u_username, b.nickname as u_nickname, b.mobile as u_mobile')
                ->where($where)
                ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
                ->order('a.order_id desc')
                ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
                ->select();
        }

        $order_ids = [];
        $OrderReminderID = [];
        foreach ($list as $key => $value) {
            array_push($order_ids, $value['order_id']);
            if (1 == $value['order_status']) array_push($OrderReminderID, $value['order_id']);
        }

        // 处理订单详情数据
        $where = [
            'a.order_id' => ['IN', $order_ids]
        ];
        $DetailsData = $this->shop_order_details_db->alias('a')
            ->field('a.*, b.service_id, b.status')
            ->where($where)
            ->join('__SHOP_ORDER_SERVICE__ b', 'a.details_id = b.details_id', 'LEFT')
            ->order('details_id asc')
            ->select();
        $ArchivesData = get_archives_data($DetailsData, 'product_id');
        $OrderServiceStatus = Config::get('global.order_service_status');
        foreach ($DetailsData as $key => $value) {
            // 商品链接
            $value['arcurl'] = get_arcurl($ArchivesData[$value['product_id']]);
            // 商品图片处理
            $value['litpic'] = handle_subdir_pic(get_default_pic($value['litpic']));
            // 售后信息处理
            $value['service_id'] = !empty($value['service_id']) ? $value['service_id'] : 0;
            $value['status'] = !empty($value['status']) ? $value['status'] : 0;
            $value['status_name'] = !empty($value['status']) && in_array($value['status'], [6, 7]) ? '维权完成' : '维权中';
            // 产品属性处理
            $goodsData = unserialize($value['data']);
            $value['product_attr'] = !empty($goodsData['product_attr']) ? $goodsData['product_attr'] : '';
            $value['product_spec'] = !empty($goodsData['product_spec']) ? $goodsData['product_spec'] : '';
            $value['product_spec'] = !empty($goodsData['product_spec']) ? "<span class='eyou_product_spec'>".str_replace("；", "</span><span class='eyou_product_spec'>", $goodsData['product_spec'])."</span>" : '';

            $DetailsData[$key] = $value;
        }

        // 把订单详情数据植入订单数据
        $defaultDetails = [
            'details_id' => 0,
            'order_id' => 0,
            'users_id' => 0,
            'product_id' => 0,
            'product_name' => '',
            'num' => 0,
            'data' => '',
            'product_price' => 0,
            'prom_type' => 0,
            'litpic' => get_default_pic(),
            'apply_service' => 0,
            'is_comment' => 0,
            'add_time' => 0,
            'update_time' => 0,
            'service_id' => 0,
            'status' => 0,
            'status_name' => '',
            'arcurl' => '',
        ];
        // 把订单详情数据植入订单数据
        $DetailsDataGroup = group_same_key($DetailsData, 'order_id');
        foreach ($list as $key => $value) {
            // 处理会员昵称
            $value['u_nickname'] = !empty($value['u_nickname']) ? $value['u_nickname'] : $value['u_username'];
            // 处理订单详情数据
            $value['Details'] = $DetailsDataGroup[$value['order_id']];
            if (empty($value['Details'])) $value['Details'] = [$defaultDetails];
            // 商品条数
            $value['rowspan'] = count($value['Details']);
            // 添加时间
            $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
            // 更新时间
            $value['update_time'] = date('Y-m-d H:i:s', $value['update_time']);
            // 处理订单主表的地址数据处理，显示中文名字
            $value['country'] = '中国';
            $value['province'] = get_province_name($value['province']);
            $value['city'] = get_city_name($value['city']);
            $value['district'] = get_area_name($value['district']);
            $value['all_address'] = "{$value['province']}-{$value['city']}-{$value['district']}-{$value['address']}";
            // 重新赋值数据
            $list[$key] = $value;
        }

        // 查询订单被提醒发货次数
        if (!empty($OrderReminderID)) {
            $field = 'order_id, count(action_id) as action_count';
            $group = 'order_id';
            $LogWhere = [
                'action_desc' => '提醒成功！',
                'order_id' => ['IN', $OrderReminderID]
            ];
            $LogData = $this->shop_order_log_db->field($field)->group($group)->where($LogWhere)->getAllWithIndex('order_id');
        }

        // // 计算单页总额
        // $total_money = $this->shop_order_db->alias('a')->where($where)->where('order_id','IN',$order_ids)->value("SUM(`order_amount`) as `total_money`");
        // /*查询已退还的总额*/
        // $list_order_id = get_arr_column($list, 'order_id');
        // $where_new = [
        //     'service_type' => 2,
        //     'status' => ['IN', [2, 4, 5, 7]],
        //     'order_id' => ['IN', $list_order_id]
        // ];
        // $refund_balance = Db::name('shop_order_service')->where($where_new)->value("SUM(`refund_balance`) as `refund_balance`");
        // /* END */
        // // 计算单页总额 - 已退还的总额
        // if (!empty($refund_balance)) $total_money = $total_money - $refund_balance;
        // $this->assign('total_money', $total_money);


        // 分页显示输出
        $pageStr = $pageObj->show();
        // 获取订单方式名称
        $pay_method_arr = Config::get('global.pay_method_arr');
        // 获取订单状态
        $admin_order_status_arr = Config::get('global.admin_order_status_arr');

        // 数据加载
        $this->assign('list', $list);
        $this->assign('page', $pageStr);
        $this->assign('pager', $pageObj);
        $this->assign('LogData', $LogData);
        $this->assign('pay_method_arr', $pay_method_arr);
        $this->assign('admin_order_status_arr', $admin_order_status_arr);

        // 是否开启文章付费
        $channelRow = Db::name('channeltype')->where('nid', 'in', ['article', 'download'])->getAllWithIndex('nid');
        foreach ($channelRow as &$val) {
            if (!empty($val['data'])) $val['data'] = json_decode($val['data'], true);
        }
        $this->assign('channelRow', $channelRow);

        // 是否开启货到付款
        $shopOpenOffline = 1;
        if (0 === intval($this->UsersConfigData['shop_open_offline']) || !isset($this->UsersConfigData['shop_open_offline'])) {
            $shopOpenOffline = 0;
        }
        $this->assign('shopOpenOffline', $shopOpenOffline);

        // 开启的商城商品类型
        $shopType = intval($this->UsersConfigData['shop_type']);
        $this->assign('shopType', $shopType);

        // 是否开启微信、支付宝支付
        $where = [
            'status' => 1,
            'pay_mark' => ['IN', ['wechat', 'alipay']]
        ];
        $payApiConfig = Db::name('pay_api_config')->where($where)->select();
        $openWeChat = $openAliPay = 1;
        foreach ($payApiConfig as $key => $value) {
            $payInfo = unserialize($value['pay_info']);
            if (!empty($payInfo) && isset($payInfo['is_open_wechat']) && 0 === intval($payInfo['is_open_wechat'])) {
                $openWeChat = 0;
            }
            if (!empty($payInfo) && isset($payInfo['is_open_alipay']) && 0 === intval($payInfo['is_open_alipay'])) {
                $openAliPay = 0;
            }
        }
        $this->assign('openWeChat', $openWeChat);
        $this->assign('openAliPay', $openAliPay);

        // 是否安装 可视化微信小程序（商城版），未安装开启则不显示小程序支付
        $where = [
            'status' => 1,
            'code' => 'DiyminiproMall'
        ];
        $openMall = Db::name('weapp')->where($where)->count();
        $this->assign('openMall', $openMall);

        return $this->fetch();
    }

    /**
     *  订单详情
     */
    public function order_details()
    {
        $order_id = input('param.order_id/d');
        if (!empty($order_id)) {
            // 查询订单信息
            $this->GetOrderData($order_id);
            // 查询订单操作记录
            $Action = $this->shop_order_log_db->where('order_id', $order_id)->order('action_id desc')->select();
            // 操作记录数据处理
            foreach ($Action as $key => $value) {
                if (0 == $value['action_user']) {
                    $Action[$key]['action_note'] = '[买家] ' . $value['action_note'];
                    // 若action_user为0，表示会员操作，根据订单号中的ID获取会员名。
                    $user = $this->users_db->field('username, nickname')->where('users_id', $value['users_id'])->find();
                    $Action[$key]['action_users'] = !empty($user['nickname']) ? $user['nickname'] : $user['username'];
                } else {
                    $Action[$key]['action_note'] = '[商家] ' . $value['action_note'];
                    // 若action_user不为0，表示操作，根据ID获取名。
                    $user_name = Db::name('admin')->where('admin_id', $value['action_user'])->getField('user_name');
                    $Action[$key]['action_users'] = !empty($user_name) ? $user_name : '';
                }
                $Action[$key]['action_desc'] = str_replace("！", "", $value['action_desc']);
                // 操作时，订单发货状态
                $Action[$key]['express_status'] = 1 == $value['express_status'] ? '已发货' : '未发货';
                // 操作时，订单付款状态
                $Action[$key]['pay_status'] = 1 == $value['pay_status'] ? '已支付' : '未支付';
            }
            $this->assign('Action', $Action);

            // 上一个页面来源
            $assign_data = [];
            $backurl = input('param.backurl/s');
            $assign_data['backurl'] = $backurl;

            // 是否弹窗打开
            $iframe = input('param.iframe/d',0);
            $assign_data['iframe'] = $iframe;
            $this->assign($assign_data);

            return $this->fetch('order_details');
        } else {
            $this->error('非法访问！');
        }
    }

    // 订单备注
    public function order_remarks()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            if (!empty($post['order_id']) && !empty($post['order_remarks'])) {
                $admin_id = session('admin_info.admin_id');
                $admin_info = Db::name('admin')->where(['admin_id'=>$admin_id])->find();
                // 执行条件
                $where = [
                    'order_id' => intval($post['order_id'])
                ];
                // 查询订单备注信息
                $adminNote = Db::name('shop_order')->where($where)->getField('admin_note');
                if (empty($adminNote)) {
                    $adminNote = [
                        'times' => date('Y-m-d H:i:s'),
                        'admin_id' => $admin_info['admin_id'],
                        'admin_name' => $admin_info['user_name'],
                        'remarks' => strval($post['order_remarks'])
                    ];
                    $update = [
                        'admin_note' => serialize([$adminNote]),
                        'update_time' => getTime()
                    ];
                } else {
                    // 解析原先的备注信息
                    $adminNote = unserialize($adminNote);
                    // 新的备注信息
                    $adminNoteNew = [
                        'times' => date('Y-m-d H:i:s'),
                        'admin_id' => $admin_info['admin_id'],
                        'admin_name' => $admin_info['user_name'],
                        'remarks' => strval($post['order_remarks'])
                    ];
                    // 合并两个备注信息
                    $adminNote = array_merge($adminNote, [$adminNoteNew]);
                    $update = [
                        'admin_note' => serialize($adminNote),
                        'update_time' => getTime()
                    ];
                }
                if (!empty($update)) $updateID = Db::name('shop_order')->where($where)->update($update);
                if (!empty($updateID)) $this->success('备注成功');
            }
            $this->error('操作失败，刷新重试！');
        }

        $order_id = input('param.order_id/d', 0);
        $this->assign('order_id', $order_id);
        $where = [
            'order_id' => intval($order_id)
        ];
        $remarksList = Db::name('shop_order')->where($where)->getField('admin_note');
        $remarksList = !empty($remarksList) ? unserialize($remarksList) : [];
        krsort($remarksList);
        $this->assign('remarksList', $remarksList);

        return $this->fetch();
    }

    /**
     *  订单发货
     */
    public function order_send()
    {
        $order_id = input('param.order_id');
        if ($order_id) {
            // 查询订单信息
            $this->GetOrderData($order_id);

            $where = [
                'is_choose' => 1,
            ];
            $express = $this->shop_express_db->where($where)->order('sort_order asc, express_id asc')->select();
            $this->assign('express', $express);
            return $this->fetch('order_send');
        }
    }

    /**
     *  订单发货操作
     */
    public function order_send_operating()
    {
        if (IS_POST) {
            $post = input('post.');
            // 参数强制转类型
            $post['order_id'] = intval($post['order_id']);
            $post['users_id'] = intval($post['users_id']);
            $post['prom_type'] = intval($post['prom_type']);
            $post['express_id'] = intval($post['express_id']);

            // 需要物流时必须选择快递
            if (isset($post['prom_type']) && 0 === $post['prom_type']) {
                if (empty($post['express_id']) || empty($post['express_name']) || empty($post['express_code'])) {
                    $this->error('请选择快递公司');
                }
            }

            // 条件数组
            $Where = [
                'order_id' => $post['order_id'],
                'users_id' => $post['users_id'],
            ];
            $OrderData = $this->shop_order_db->where($Where)->find();

            // 更新数组
            $UpdateData = [
                'order_status' => 2,
                'express_order' => $post['express_order'],
                'express_name' => $post['express_name'],
                'express_code' => $post['express_code'],
                'express_time' => getTime(),
                'consignee' => $post['consignee'],
                'update_time' => getTime(),
                'virtual_delivery' => $post['virtual_delivery'],
            ];

            // 订单操作记录逻辑
            $LogWhere = [
                'order_id' => $post['order_id'],
                'express_status' => 1,
            ];
            $LogData = $this->shop_order_log_db->where($LogWhere)->count();
            if (!empty($LogData)) {
                // 数据存在则表示为修改发货内容
                $Desc = '修改发货内容！';
                if (1 == $post['prom_type']) {
                    // 提交的数据为虚拟订单
                    if ($OrderData['prom_type'] != $post['prom_type']) {
                        // 此处判断后，提交的订单类型和数据库中的订单类型不相同，表示普通订单修改为虚拟订单
                        $Note = '将普通订单修改为虚拟订单！';
                        if (!empty($post['virtual_delivery'])) {
                            // 若存在数据则拼装
                            $Note .= '给买家回复：' . $post['virtual_delivery'];
                        }
                    } else {
                        // 继续保持为虚拟订单修改
                        $Note = '虚拟订单，无需物流。';
                        if (!empty($post['virtual_delivery'])) {
                            // 若存在数据则拼装
                            $Note .= '给买家回复：' . $post['virtual_delivery'];
                        }
                    }
                } else {
                    // 提交的数据为普通订单
                    if ($OrderData['prom_type'] != $post['prom_type']) {
                        // 这一段暂时无用，因为发货时，暂时无法选择将虚拟订单修改为普通订单
                        $Note = '将虚拟订单修改为普通订单！';
                        if (!empty($post['virtual_delivery'])) {
                            // 若存在数据则拼装
                            $Note .= '给买家回复：' . $post['virtual_delivery'];
                        }
                    } else {
                        // 继续保持为普通订单修改
                        $Note = '使用' . $post['express_name'] . '发货成功！';
                    }
                }
                $UpdateData['prom_type'] = $post['prom_type'];
            } else {
                // 数据不存在则表示为初次发货，拼装发货内容
                $Desc = '发货成功！';
                $Note = '使用' . $post['express_name'] . '发货成功！';
                if ('1' == $post['prom_type']) {
                    // 若为虚拟订单，无需发货物流。
                    $UpdateData['prom_type'] = $post['prom_type'];
                    $Note = '虚拟订单，无需物流。';
                    if (!empty($post['virtual_delivery'])) {
                        // 若存在数据则拼装
                        $Note .= '给买家回复：' . $post['virtual_delivery'];
                    }
                }
            }

            // 配送单号
            if (empty($post['prom_type']) && empty($post['express_order'])) $this->error('配送单号不能为空');

            // 更新订单主表信息
            $IsOrder = $this->shop_order_db->where($Where)->update($UpdateData);
            if (!empty($IsOrder)) {
                // 更新订单明细表信息
                $Data['update_time'] = getTime();
                $this->shop_order_details_db->where('order_id', $post['order_id'])->update($Data);

                // 添加订单操作记录
                AddOrderAction($post['order_id'], 0, session('admin_id'), 2, 1, 1, $Desc, $Note);

                // 查询会员信息
                $Field = 'users_id, username, nickname, email, mobile';
                $Users = $this->users_db->field($Field)->where('users_id', $post['users_id'])->find();

                // 邮箱发送
                // $SmtpConfig = tpCache('smtp');
                // $Result['email'] = GetEamilSendData($SmtpConfig, $Users, $post, 2);

                // 订单发货通知
                $Result['notice'] = [
                    'send_scene' => 6,
                    'users_id' => $Users['users_id'],
                    'order_id' => $OrderData['order_id'],
                ];

                // 发送站内信给会员
                // $NickName = !empty($Users['nickname']) ? $Users['nickname'] : $Users['username'];
                // SendNotifyMessage($UpdateData, 6, 0, $post['users_id'], $NickName);

                $this->success('发货成功', null, $Result);
            } else {
                $this->error('发货失败');
            }
        }
    }

    // 订单更新收货地址
    public function order_address()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // 判断数据
            if (empty($post['consignee'])) $this->error('请填写收货人姓名！');
            if (empty($post['mobile'])) $this->error('请填写收货人手机！');
            if (empty($post['province'])) $this->error('请选择省份！');
            if (empty($post['city'])) $this->error('请选择城市！');
            if (empty($post['district'])) $this->error('请选择县区！');
            if (empty($post['address'])) $this->error('请填写详细地址！');

            // 更新地址
            $where = [
                'order_id' => intval($post['order_id']),
            ];
            $update = [
                'consignee' => strval($post['consignee']),
                'mobile' => strval($post['mobile']),
                'province' => intval($post['province']),
                'city' => intval($post['city']),
                'district' => intval($post['district']),
                'address' => strval($post['address']),
                'update_time' => getTime()
            ];
            $updateID = Db::name('shop_order')->where($where)->update($update);
            if (!empty($updateID)) {
                // 添加订单操作记录
                AddOrderAction($post['order_id'], 0, session('admin_id'), 1, 0, 1, '修改地址！', '修改买家订单收货信息！');
                $this->success('更新成功');
            } else {
                $this->error('操作失败，刷新重试！');
            }
        }

        $order_id = input('param.order_id/d', 0);
        $this->assign('order_id', $order_id);
        $address = Db::name('shop_order')->field('consignee, mobile, province, city, district, address')->where($where)->find($order_id);
        $this->assign('address', $address);
        $provinceList = get_province_list();
        $this->assign('provinceList', $provinceList);
        $cityList = Db::name('region')->where('parent_id', $address['province'])->select();
        $this->assign('cityList', $cityList);
        $areaList = Db::name('region')->where('parent_id', $address['city'])->select();
        $this->assign('areaList', $areaList);

        return $this->fetch();
    }

    // 联动地址获取
    public function ajaxGetSpecifyRegion()
    {
        $parent_id  = input('param.parent_id/d');
        $region = Db::name('region')->where("parent_id", $parent_id)->select();
        $html = '';
        if (!empty($region)) {
            // 拼装下拉选项
            foreach ($region as $value) {
                $html .= "<option value='{$value['id']}'>{$value['name']}</option>";
            }
        }
        $this->success('查询成功！', null, $html);
    }

    /**
     * 查询快递名字及Code
     */
    public function order_express()
    {
        // 查询条件
        $where = [];
        $keywords = input('keywords/s');
        if (!empty($keywords)) $where['express_name'] = ['LIKE', "%{$keywords}%"];

        // 分页查询
        $count = $this->shop_express_db->where($where)->count('express_id');
        $pager = new Page($count, 50);

        // 查询数据
        $ExpressData = $this->shop_express_db->where($where)
            ->limit($pager->firstRow . ',' . $pager->listRows)
            ->order('is_choose desc, sort_order asc')
            ->select();

        // 计算选中个数
        $where['is_choose'] = 1;
        $selectNum = $this->shop_express_db->where($where)->count();
        $this->assign('selectNum', $selectNum);

        // 加载模板
        $show = $pager->show();
        $select = input('param.select/d', 0);
        $this->assign('select', $select);
        $this->assign('page', $show);
        $this->assign('pager', $pager);
        $this->assign('ExpressData', $ExpressData);
        return $this->fetch('order_express');
    }

    // 全部选中/全部取消
    public function express_is_choose()
    {
        if (IS_AJAX_POST) {
            $is_choose = input('post.is_choose/d', 0);
            if (isset($is_choose) && $is_choose >= 0) {
                $where = [
                    'is_choose' => !empty($is_choose) ? 0 : 1
                ];
                $update = [
                    'is_choose' => $is_choose,
                    'update_time' => getTime(),
                ];
                $ResultID = $this->shop_express_db->where($where)->update($update);
                if (!empty($ResultID)) $this->success('操作成功');
            }
        }
        $this->error('操作失败');
    }

    /**
     *  后台标记订单状态
     */
    public function order_mark_status()
    {
        if (IS_POST) {
            $post = input('post.');
            // 条件数组
            $Where = [
                'order_id' => $post['order_id'],
                'users_id' => $post['users_id'],
            ];

            if ('ddsc' == $post['status_name']) {
                // 订单删除
                $IsDelete = $this->shop_order_db->where($Where)->delete();
                if (!empty($IsDelete)) {
                    $Where = [
                        'order_id' => $post['order_id'],
                    ];
                    // 同步删除订单下的操作记录
                    $this->shop_order_log_db->where($Where)->delete();
                    // 同步删除订单下的产品
                    $this->shop_order_details_db->where($Where)->delete();
                    $this->success('删除成功！', url('Shop/index'));
                } else {
                    $this->error('数据错误！');
                }
            } else {
                $OrderData = $this->shop_order_db->where($Where)->find();

                // 更新数组
                $UpdateData = [
                    'update_time' => getTime(),
                ];

                // 根据不同操作标记不同操作内容
                if ('yfk' == $post['status_name']) {
                    // 订单标记为付款，追加更新数组
                    $UpdateData['order_status'] = '1';
                    $UpdateData['pay_time'] = getTime();
                    // 付款
                    $UpdateData['pay_name'] = 'admin_pay';

                    /*用于添加订单操作记录*/
                    $order_status = '1'; // 订单状态
                    $express_status = '0'; // 发货状态
                    $pay_status = '1'; // 支付状态
                    $action_desc = '付款成功！'; // 操作明细
                    $action_note = '确认订单付款！'; // 操作备注
                    /*结束*/

                } else if ('ysh' == $post['status_name']) {
                    // 订单确认收货，追加更新数组
                    $UpdateData['order_status'] = '3';
                    $UpdateData['confirm_time'] = getTime();

                    /*用于添加订单操作记录*/
                    $order_status = '3'; // 订单状态
                    $express_status = '1'; // 发货状态
                    $pay_status = '1'; // 支付状态
                    $action_desc = '确认收货！'; // 操作明细
                    $action_note = '确认订单已收货！'; // 操作备注
                    /*结束*/

                } else if ('gbdd' == $post['status_name']) {
                    // 订单关闭，追加更新数组
                    $UpdateData['order_status'] = '-1';

                    /*用于添加订单操作记录*/
                    $order_status = '-1'; // 订单状态
                    if ('0' == $OrderData['order_status'] || '1' == $OrderData['order_status']) {
                        $express_status = '0'; // 发货状态
                        $pay_status = '0'; // 支付状态
                    } else {
                        $express_status = '1'; // 发货状态
                        $pay_status = '1'; // 支付状态
                    }
                    $action_desc = '订单取消！'; // 操作明细
                    $action_note = '关闭订单！'; // 操作备注
                    /*结束*/
                }

                // 更新订单主表
                $IsOrder = $this->shop_order_db->where($Where)->update($UpdateData);
                if (!empty($IsOrder)) {
                    // 更新订单明细表
                    $Data['update_time'] = getTime();
                    $this->shop_order_details_db->where('order_id', $post['order_id'])->update($Data);

                    // 如果是关闭订单操作则执行还原产品库存
                    if ('gbdd' == $post['status_name']) {
                        if (isset($OrderData['order_stock_type']) && 0 === intval($OrderData['order_stock_type'])) {
                            model('ShopGoods')->goodsStockRestore($OrderData['order_id'], $OrderData['users_id']);
                        }
                    }

                    // 添加订单操作记录
                    AddOrderAction($post['order_id'], 0, session('admin_id'), $order_status, $express_status, $pay_status, $action_desc, $action_note);

                    // 订单关闭通知
                    $params = [
                        'users_id' => $post['users_id'],
                        'result_id' => $post['order_id'],
                    ];
                    eyou_send_notice(9, $params, ['buyer']);

                    // 判断是否为虚拟商品
                    if ('yfk' == $post['status_name'] && $OrderData['prom_type'] == 1) {
                        PayModel::afterVirtualProductPay($Where);
                    }
                    $this->success('操作成功！', url('Shop/index'));
                }
            }
        } else {
            $this->error('非法访问！');
        }
    }

    // 手动关闭订单并退款
    public function order_manual_refund()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            $OrderID = intval($post['order_id']);
            $RefundNote = trim($post['refund_note']);
            if (!empty($OrderID)) {
                $update = [
                    'order_id' => $OrderID,
                    'order_status' => '-1',
                    'manual_refund' => 1,
                    'refund_note' => $RefundNote,
                    'update_time' => getTime(),
                ];
                $ResultID = $this->shop_order_db->update($update);
                if (!empty($ResultID)) {
                    // 添加订单操作记录
                    AddOrderAction($OrderID, 0, session('admin_id'), '-1', 0, 1, '关闭并退款', '手动关闭订单并自行退款');
                    // 返回结束
                    $this->success('操作完成');
                }
            }
        }
        $this->error('非法访问！');
    }

    /*
     *  更新备注
     */
    public function update_note()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            if (!empty($post['order_id'])) {
                $UpdateData = [
                    'admin_note' => $post['admin_note'],
                    'update_time' => getTime(),
                ];
                $return = $this->shop_order_db->where('order_id', $post['order_id'])->update($UpdateData);
                if (!empty($return)) {
                    $this->success('保存成功！');
                }
            } else {
                $this->error('非法访问！');
            }
        } else {
            $this->error('非法访问！');
        }
    }

    /*
     *  运费模板列表
     */
    public function shipping_template()
    {
        $Where = [
            'a.level' => 1,
        ];

        $region_name = input('param.region_name');
        if (!empty($region_name)) {
            $Where['a.name'] = array('LIKE', "%{$region_name}%");
        }

        // 省份
        $Template = Db::name('region')->field('a.id, a.name,b.template_money,b.template_id')
            ->alias('a')
            ->join('__SHOP_SHIPPING_TEMPLATE__ b', 'a.id = b.province_id', 'LEFT')
            ->where($Where)
            ->getAllWithIndex('id');
        $this->assign('Template', $Template);

        // 统一配送
        $info = $this->shipping_template_db->where('province_id', '100000')->find();
        $this->assign('info', $info);

        return $this->fetch('shipping_template');
    }

    // 订单批量删除
    public function order_del()
    {
        $order_id = input('del_id/a');
        $order_id = eyIntval($order_id);
        if (IS_AJAX_POST && !empty($order_id)) {
            // 条件数组
            $where = [
                'order_id' => ['IN', $order_id],
            ];
            // 查询数据，存在adminlog日志
            $result = $this->shop_order_db->field('order_code')->where($where)->select();
            $order_code_list = get_arr_column($result, 'order_code');
            // 删除订单列表数据
            $return = $this->shop_order_db->where($where)->delete(true);
            if ($return) {
                // 同步删除订单下的产品
                $this->shop_order_details_db->where($where)->delete(true);
                // 同步删除订单下的操作记录
                $this->shop_order_log_db->where($where)->delete(true);
                
                // 同步删除订单维权数据
                Db::name('shop_order_service')->where($where)->delete(true);
                // 同步删除订单维权记录数据
                Db::name('shop_order_service_log')->where($where)->delete(true);

                adminLog('删除订单：' . implode(',', $order_code_list));
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
        $this->error('参数有误');
    }

    /*
     *  查询会员订单数据并加载，无返回
     */
    function GetOrderData($order_id)
    {
        // 获取订单数据
        $OrderData = $this->shop_order_db->find($order_id);
        $OrderData['add_time'] = date('Y-m-d H:i:s', $OrderData['add_time']);
        $OrderData['pay_time'] = !empty($OrderData['pay_time']) ? date('Y-m-d H:i:s', $OrderData['pay_time']) : 0;
        $OrderData['update_time'] = !empty($OrderData['update_time']) ? date('Y-m-d H:i:s', $OrderData['update_time']) : 0;
        $OrderData['express_time'] = !empty($OrderData['express_time']) ? date('Y-m-d H:i:s', $OrderData['express_time']) : 0;
        $OrderData['confirm_time'] = !empty($OrderData['confirm_time']) ? date('Y-m-d H:i:s', $OrderData['confirm_time']) : 0;
        $OrderData['order_terminal_name'] = '电脑端';
        if (!empty($OrderData['order_terminal']) && 2 === intval($OrderData['order_terminal'])) {
            $OrderData['order_terminal_name'] = '手机端';
        } else if (!empty($OrderData['order_terminal']) && 3 === intval($OrderData['order_terminal'])) {
            $OrderData['order_terminal_name'] = '微信小程序';
        }

        // 获取会员数据
        $UsersData = $this->users_db->find($OrderData['users_id']);
        $UsersData['username'] = !empty($UsersData['nickname']) ? $UsersData['nickname'] : $UsersData['username'];
        // 当前单条订单信息的会员ID，存入session，用于添加订单操作表
        session('OrderUsersId', $OrderData['users_id']);

        // 获取订单详细表数据
        $DetailsData = $this->shop_order_details_db->where('order_id', $OrderData['order_id'])->select();

        // 获取订单状态，后台专用
        $admin_order_status_arr = Config::get('global.admin_order_status_arr');

        // 获取订单方式名称
        $pay_method_arr = Config::get('global.pay_method_arr');

        // 处理订单主表的地址数据处理，显示中文名字
        $OrderData['country'] = '中国';
        $OrderData['province'] = get_province_name($OrderData['province']);
        $OrderData['city'] = get_city_name($OrderData['city']);
        $OrderData['district'] = get_area_name($OrderData['district']);

        $array_new = get_archives_data($DetailsData, 'product_id');
        $OrderData['prom_type_virtual'] = false;
        // 处理订单详细表数据处理
        foreach ($DetailsData as $key => $value) {
            if ($value['prom_type'] == 1) $OrderData['prom_type_virtual'] = true;
            // 商品规格、属性处理
            $goodsData = unserialize($value['data']);
            $value['product_attr'] = !empty($goodsData['product_attr']) ? $goodsData['product_attr'] : '';
            $value['product_spec'] = !empty($goodsData['product_spec']) ? "<span class='eyou_product_spec'>".str_replace("；", "</span><span class='eyou_product_spec'>", $goodsData['product_spec'])."</span>" : '';
            $value['arcurl'] = get_arcurl($array_new[$value['product_id']]);
            $value['litpic'] = handle_subdir_pic($DetailsData[$key]['litpic']);
            $value['subtotal'] = sprintf("%.2f", floatval($value['product_price']) * floatval($value['num']));

            $DetailsData[$key] = $value;
        }

        // 订单类型
        $OrderData['prom_type_name'] = empty($OrderData['prom_type']) ? '普通订单' : '虚拟订单';

        // 移动端查询物流链接
        $MobileExpressUrl = "//m.kuaidi100.com/index_all.html?type=" . $OrderData['express_code'] . "&postid=" . $OrderData['express_order'];

        $adminNoteCount = !empty($OrderData['admin_note']) ? count(unserialize($OrderData['admin_note'])) : 0;
        $this->assign('adminNoteCount', $adminNoteCount);

        // 加载数据
        $this->assign('OrderData', $OrderData);
        $this->assign('UsersData', $UsersData);
        $this->assign('DetailsData', $DetailsData);
        $this->assign('pay_method_arr', $pay_method_arr);
        $this->assign('MobileExpressUrl', $MobileExpressUrl);
        $this->assign('admin_order_status_arr', $admin_order_status_arr);
    }

    // ------------------------------------------------------------------------------------------------------
    // 以下所有代码都是产品规格处理逻辑 2019-07-08 陈风任
    // ------------------------------------------------------------------------------------------------------
    // 规格列表管理，包含新增、更新
    public function spec_index()
    {
        if (IS_AJAX_POST) {
            // 新增、更新
            $post = input('post.');
            // 当前时间戳
            $time = getTime();
            /*新增数据处理*/
            $post_new = [];
            foreach ($post['preset_new'] as $key => $value) {
                // 规格名称不允许为空
                $preset_name = $post['preset_name_' . $value][0];
                if (empty($preset_name)) continue;
                // 是否同步到已发布的商品规格
                $spec_sync = !empty($post['spec_sync_' . $value]) ? 1 : 0;
                // 排序号
                $sort_order = $post['sort_order_' . $value];

                // 拼装三维数组
                foreach ($post['preset_value_' . $value] as $kk => $vv) {
                    if (empty($vv)) continue;
                    $post_new[$key][$kk]['preset_mark_id'] = $value; // 标记ID，一整条规格信息中的唯一标识
                    $post_new[$key][$kk]['preset_name'] = $preset_name;
                    $post_new[$key][$kk]['preset_value'] = $vv;
                    $post_new[$key][$kk]['spec_sync'] = $spec_sync;
                    $post_new[$key][$kk]['sort_order'] = $sort_order;
                    $post_new[$key][$kk]['add_time'] = $time;
                    $post_new[$key][$kk]['update_time'] = $time;
                }
            }

            // 三维数组降为二维数组
            $data_new = $this->ProductSpecLogic->ArrayDowngrade($post_new);
            /* END */

            /*原有数据处理*/
            $post_old = [];
            $spec_sync_where = $spec_sync_mark_data = $spec_sync_value_data = [];
            foreach ($post['preset_old'] as $key => $value) {
                // 规格名称不允许为空
                $preset_name = $post['preset_name_old_' . $value][0];
                if (empty($preset_name)) continue;

                // 是否同步到已发布的商品规格
                $spec_sync = !empty($post['spec_sync_' . $value]) ? 1 : 0;

                // 排序号
                $sort_order = $post['sort_order_' . $value];

                // 需要同步的规格名称
                if (!empty($spec_sync)) {
                    array_push($spec_sync_where, $value);
                    $spec_sync_mark_data[$value]['spec_name'] = $preset_name;
                }

                // 拼装三维数组
                foreach ($post['preset_value_old_' . $value] as $kk => $vv) {
                    if (empty($vv)) continue;
                    $preset_id = $post['preset_id_old_' . $value][$kk];
                    // 如果ID是否为空
                    if (!empty($preset_id)) {
                        // 有ID则为更新
                        $post_old[$key][$kk]['preset_id'] = $preset_id;

                        // 需要同步的规格值
                        if (!empty($spec_sync)) $spec_sync_value_data[$preset_id]['spec_value'] = $vv;
                    } else {
                        // 无ID则为新增
                        $post_old[$key][$kk]['add_time'] = $time;
                        $post_old[$key][$kk]['preset_mark_id'] = $value; // 标记ID，一整条规格信息中的唯一标识
                    }
                    $post_old[$key][$kk]['preset_name'] = $preset_name;
                    $post_old[$key][$kk]['preset_value'] = $vv;
                    $post_old[$key][$kk]['spec_sync'] = $spec_sync;
                    $post_old[$key][$kk]['sort_order'] = $sort_order;
                    $post_old[$key][$kk]['update_time'] = $time;
                }
            }
            // 三维数组降为二维数组
            $data_old = $this->ProductSpecLogic->ArrayDowngrade($post_old);
            /* END */

            // 合并数组并且更新数据
            $UpData = array_merge($data_old, $data_new);
            model('ProductSpecPreset')->saveAll($UpData);

            /*执行同步数据*/
            if (!empty($spec_sync_where)) {
                $w_1['spec_mark_id'] = ['IN', $spec_sync_where];
                $f_1 = 'spec_id, spec_mark_id, spec_name, spec_value_id, spec_value';
                $SpecMarkData = Db::name('product_spec_data')->where($w_1)->field($f_1)->order('spec_mark_id asc')->select();
                foreach ($SpecMarkData as $key => $value) {
                    $SpecMarkData[$key]['spec_name'] = $spec_sync_mark_data[$value['spec_mark_id']]['spec_name'];
                    $SpecMarkData[$key]['spec_value'] = $spec_sync_value_data[$value['spec_value_id']]['spec_value'];
                }
                model('ProductSpecData')->saveAll($SpecMarkData);
            }
            /* END */

            $this->success('更新成功！');
        }

        // 查询规格数据
        $PresetData = $this->product_spec_preset_db->order('sort_order asc, preset_id asc')->select();
        // 数组转化
        $ResultData = $this->ProductSpecLogic->GetPresetData($PresetData);
        // 获取预设规格中最大的标记MarkId
        $PresetMarkId = model('ProductSpecPreset')->GetMaxPresetMarkId();
        // 加载参数
        $this->assign('info', $ResultData);
        $this->assign('PresetMarkId', $PresetMarkId);
        return $this->fetch('spec_index');
    }


    // 规格列表管理，包含新增、更新
    public function spec_template()
    {
        if (IS_AJAX_POST) {
            // 新增、更新
            $post = input('post.');
            // 当前时间戳
            $time = getTime();
            /*新增数据处理*/
            $post_new = [];
            foreach ($post['preset_new'] as $key => $value) {
                // 规格名称不允许为空
                $preset_name = $post['preset_name_' . $value][0];
                if (empty($preset_name)) continue;
                // 是否同步到已发布的商品规格
                $spec_sync = !empty($post['spec_sync_' . $value]) ? 1 : 0;
                // 排序号
                $sort_order = $post['sort_order_' . $value];

                // 拼装三维数组
                foreach ($post['preset_value_' . $value] as $kk => $vv) {
                    if (empty($vv)) continue;
                    $post_new[$key][$kk]['preset_mark_id'] = $value; // 标记ID，一整条规格信息中的唯一标识
                    $post_new[$key][$kk]['preset_name'] = $preset_name;
                    $post_new[$key][$kk]['preset_value'] = $vv;
                    $post_new[$key][$kk]['spec_sync'] = $spec_sync;
                    $post_new[$key][$kk]['sort_order'] = $sort_order;
                    $post_new[$key][$kk]['add_time'] = $time;
                    $post_new[$key][$kk]['update_time'] = $time;
                }
            }

            // 三维数组降为二维数组
            $data_new = $this->ProductSpecLogic->ArrayDowngrade($post_new);
            /* END */

            /*原有数据处理*/
            $post_old = [];
            $spec_sync_where = $spec_sync_mark_data = $spec_sync_value_data = [];
            foreach ($post['preset_old'] as $key => $value) {
                // 规格名称不允许为空
                $preset_name = $post['preset_name_old_' . $value][0];
                if (empty($preset_name)) continue;

                // 是否同步到已发布的商品规格
                $spec_sync = !empty($post['spec_sync_' . $value]) ? 1 : 0;

                // 排序号
                $sort_order = $post['sort_order_' . $value];

                // 需要同步的规格名称
                if (!empty($spec_sync)) {
                    array_push($spec_sync_where, $value);
                    $spec_sync_mark_data[$value]['spec_name'] = $preset_name;
                }

                // 拼装三维数组
                foreach ($post['preset_value_old_' . $value] as $kk => $vv) {
                    if (empty($vv)) continue;
                    $preset_id = $post['preset_id_old_' . $value][$kk];
                    // 如果ID是否为空
                    if (!empty($preset_id)) {
                        // 有ID则为更新
                        $post_old[$key][$kk]['preset_id'] = $preset_id;

                        // 需要同步的规格值
                        if (!empty($spec_sync)) $spec_sync_value_data[$preset_id]['spec_value'] = $vv;
                    } else {
                        // 无ID则为新增
                        $post_old[$key][$kk]['add_time'] = $time;
                        $post_old[$key][$kk]['preset_mark_id'] = $value; // 标记ID，一整条规格信息中的唯一标识
                    }
                    $post_old[$key][$kk]['preset_name'] = $preset_name;
                    $post_old[$key][$kk]['preset_value'] = $vv;
                    $post_old[$key][$kk]['spec_sync'] = $spec_sync;
                    $post_old[$key][$kk]['sort_order'] = $sort_order;
                    $post_old[$key][$kk]['update_time'] = $time;
                }
            }
            // 三维数组降为二维数组
            $data_old = $this->ProductSpecLogic->ArrayDowngrade($post_old);
            /* END */

            // 合并数组并且更新数据
            $UpData = array_merge($data_old, $data_new);
            model('ProductSpecPreset')->saveAll($UpData);

            /*执行同步数据*/
            if (!empty($spec_sync_where)) {
                $w_1['spec_mark_id'] = ['IN', $spec_sync_where];
                $f_1 = 'spec_id, spec_mark_id, spec_name, spec_value_id, spec_value';
                $SpecMarkData = Db::name('product_spec_data')->where($w_1)->field($f_1)->order('spec_mark_id asc')->select();
                foreach ($SpecMarkData as $key => $value) {
                    $SpecMarkData[$key]['spec_name'] = $spec_sync_mark_data[$value['spec_mark_id']]['spec_name'];
                    $SpecMarkData[$key]['spec_value'] = $spec_sync_value_data[$value['spec_value_id']]['spec_value'];
                }
                model('ProductSpecData')->saveAll($SpecMarkData);
            }
            /* END */

            $this->success('更新成功！');
        }

        // 查询规格数据
        $PresetData = $this->product_spec_preset_db->order('sort_order asc, preset_id asc')->select();
        // 数组转化
        $ResultData = $this->ProductSpecLogic->GetPresetData($PresetData);
        // 获取预设规格中最大的标记MarkId
        $PresetMarkId = model('ProductSpecPreset')->GetMaxPresetMarkId();
        // 加载参数
        $this->assign('info', $ResultData);
        $this->assign('PresetMarkId', $PresetMarkId);
        return $this->fetch('spec_template');
    }

    // 删除规格名称\规格值
    public function spec_delete()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            $where = $this->ProductSpecLogic->GetDeleteSpecWhere($post);
            if (!empty($where)) {
                $result = $this->product_spec_preset_db->where($where)->delete();
                if (!empty($result)) {
                    $this->success('删除成功！');
                }
            }
            $this->error('删除失败！');
        }
    }

    // 选中规格名称，追加html到页面展示
    public function spec_select()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 当选中的规格大类大于等于三个则不允许再添加
            if (3 == count(session('spec_arr'))) $this->error('最多只能添加三种规格大类');

            // 获取预设规格标记ID数组
            $PresetMarkIdArray = $this->ProductSpecLogic->GetPresetMarkIdArray($post);

            // 拼装预设名称下拉选项
            if (!empty($PresetMarkIdArray)) {
                // 添加选中的规格数据
                model('ProductSpecData')->PresetSpecAddData($post);
                // 拼装更新预设名称下拉选项
                $Result = $this->ProductSpecLogic->GetPresetNameOption($PresetMarkIdArray, $post);
            } else {
                $this->error('最多只能添加三种规格大类！');
            }

            if (isset($post['aid']) && !empty($post['aid'])) {
                $ResultData = $this->ProductSpecLogic->GetPresetValueOption('', $post['spec_mark_id'], $post['aid'], 2);
                $PresetName = $ResultData['PresetName'];
                $PresetValueOption = $ResultData['PresetValueOption'];
            } else {
                // 拼装预设值下拉选项
                $PresetValue = $this->product_spec_preset_db->where('preset_mark_id', 'IN', $post['preset_mark_id'])->field('preset_id,preset_name,preset_value')->select();
                $PresetName = $PresetValue[0]['preset_name'];
                $PresetValueOption = $this->ProductSpecLogic->GetPresetValueOption($PresetValue);
            }

            if (isset($post['aid']) && !empty($post['aid'])) {
                // 结果返回
                $ReturnHtml = [
                    'preset_name' => $PresetName,
                    'preset_name_option' => $Result['Option'],
                    'spec_mark_id_arr' => $Result['MarkId'],
                    'preset_value_option' => $PresetValueOption,
                ];
            } else {
                // 结果返回
                $ReturnHtml = [
                    'preset_name' => $PresetName,
                    'preset_name_option' => $Result['Option'],
                    'preset_mark_id_arr' => $Result['MarkId'],
                    'preset_value_option' => $PresetValueOption,
                ];
            }
            $this->success('加载成功！', null, $ReturnHtml);
        }
    }

    // 当规格库更新后，调用此方式及时更新选择预设规格的下拉框信息及规格框信息
    public function update_spec_info()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 拼装更新预设名称下拉选项
            $ResultData = $this->ProductSpecLogic->GetPresetNameOption($post['preset_mark_id_arr']);

            // 获取规格拼装后的html表格
            $ResultArray = $this->ProductSpecLogic->GetPresetSpecAssembly($post);

            // 结果返回
            if (isset($post['aid']) && !empty($post['aid'])) {
                $ReturnHtml = [
                    'HtmlTable' => $ResultArray['HtmlTable'],
                    'spec_name_option' => $ResultData['Option'],
                    'spec_mark_id_arr' => $ResultArray['PresetMarkIdArray'],
                ];
            } else {
                // 拼装更新预设名称下拉选项
                $where = [
                    'preset_mark_id' => ['IN', $post['preset_mark_id_arr']],
                ];
                $PresetData = $this->product_spec_preset_db->where($where)->order('preset_id asc')->select();
                $sessionData = session('spec_arr');
                foreach ($PresetData as $key => $value) {
                    if (!empty($sessionData[$value['preset_mark_id']])) {
                        if (in_array($value['preset_id'], $sessionData[$value['preset_mark_id']])) {
                            unset($PresetData[$key]);
                        }
                    }
                }

                $PresetData = group_same_key($PresetData, 'preset_mark_id');
                $result = [];
                foreach ($PresetData as $key => $value) {
                    $result[$key] .= "<option value='0'>选择规格值</option>";
                    if (!empty($value)) {
                        foreach ($value as $sub_value) {
                            $result[$key] .= "<option value='{$sub_value['preset_id']}'>{$sub_value['preset_value']}</option>";
                        }
                    }
                }

                $ReturnHtml = [
                    'HtmlTable' => $ResultArray['HtmlTable'],
                    'preset_name_option' => $ResultData['Option'],
                    'preset_mark_id_arr' => $ResultArray['PresetMarkIdArray'],
                    'preset_value_id' => explode(',', $post['preset_mark_id_arr']),
                    'preset_value_option' => $result,
                ];
            }
            $this->success('更新成功！', null, $ReturnHtml);
        }
    }

    // 获取或更新规格组合的数据
    // preset_id：预设值ID
    // preset_mark_id：预设参数标记ID，同一预设规格名称下的所有规格值统一使用，可理解为规格名称唯一ID。
    public function assemble_spec_data()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 刷新或重新进入产品添加页则清除关于产品session
            if (isset($post['initialization']) && !empty($post['initialization'])) {
                session('spec_arr', null);
                $this->success('初始化完成');
            }

            // 若清除一整条规格信息则清除session中相应的数据并且重置规格名称下拉框选项
            $ResultArray = $this->ProductSpecLogic->GetResetPresetNameOption($post);

            // 删除单个规格值则清除session对应的值
            $ValueArray = $this->ProductSpecLogic->ClearSpecValueID($post);

            // 把session中的数据和提交的数据组合
            $SpecArray = $this->ProductSpecLogic->GetSessionPostArrayMerge($post);
            if (isset($SpecArray['error']) && !empty($SpecArray['error'])) $this->error($SpecArray['error']);

            // 获取规格拼装后的html表格
            if (isset($post['aid']) && !empty($post['aid'])) {
                // 编辑
                $HtmlTable = $this->ProductSpecLogic->SpecAssemblyEdit($SpecArray, $post['aid']);
            } else {
                // 新增
                $HtmlTable = $this->ProductSpecLogic->SpecAssembly($SpecArray);
            }

            if (!empty($ValueArray['Option'])) {
                // 删除规格值后的规格值下拉框
                $PresetValueOption = $ValueArray['Option'];
                $ResultValue['Value'] = null;
            } else {
                $ResultValue = model('ProductSpecPreset')->GetPresetNewData(session('spec_arr'), $post);
                // 获取新增规格值后的下拉框
                if (empty($post['aid'])) {
                    $PresetValueOption = $this->ProductSpecLogic->GetPresetValueOption($ResultValue['Option']);
                } else {
                    $PresetValueOption = $ResultValue['Option'];
                }
            }

            // 返回数据
            $ReturnData = [
                'HtmlTable' => $HtmlTable,
                'PresetNameOption' => $ResultArray['Option'],
                'PresetMarkIdArray' => $ResultArray['MarkId'],
                'SelectPresetValue' => $ResultValue['Value'],
                'PresetValueOption' => $PresetValueOption,
            ];
            $this->success('加载成功！', null, $ReturnData);
        }
    }

    // 同步规格值到产品规格中，刷新规格值下拉框
    public function refresh_spec_value()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // 是否提交完整数据
            if (empty($post['spec_mark_id']) || empty($post['aid'])) $this->error('数据有误，同步失败，请刷新重试！');

            /*查询产品已选规格数据*/
            $where = [
                'aid' => $post['aid'],
                'spec_mark_id' => $post['spec_mark_id'],
            ];
            $SpecData = Db::name('product_spec_data')->where($where)->order('spec_value_id asc')->select();
            // 以规格值ID为键名
            $SpecData = group_same_key($SpecData, 'spec_value_id');
            /* END */

            /*查询规格库数据*/
            $where = [
                'preset_mark_id' => $post['spec_mark_id'],
            ];
            $PresetData = $this->product_spec_preset_db->where($where)->order('preset_id asc')->select();
            /* END */

            // 初始化数组
            $AddData = $UpData = $SpecIds = $UpSpecWhere = $UpSpecName = [];
            // 数据处理
            foreach ($PresetData as $pd_k => $pd_v) {
                if (!empty($SpecData[$pd_v['preset_id']]) && $pd_v['preset_name'] != $SpecData[$pd_v['preset_id']][0]['spec_name']) {
                    // 更新规格名称数组
                    if (empty($UpSpecWhere) && empty($UpSpecName)) {
                        $UpSpecWhere = [
                            'aid' => $post['aid'],
                            'spec_mark_id' => $pd_v['preset_mark_id'],
                        ];
                        $UpSpecName = [
                            'spec_name' => $pd_v['preset_name'],
                        ];
                    }
                }

                if (empty($SpecData[$pd_v['preset_id']])) {
                    // 添加规格值数据
                    $AddData[] = [
                        'aid' => $post['aid'],
                        'spec_mark_id' => $pd_v['preset_mark_id'],
                        'spec_name' => $pd_v['preset_name'],
                        'spec_value_id' => $pd_v['preset_id'],
                        'spec_value' => $pd_v['preset_value'],
                        'spec_is_select' => 0,
                        'add_time' => getTime(),
                        'update_time' => getTime(),
                    ];
                } else if (!empty($SpecData[$pd_v['preset_id']]) && $pd_v['preset_value'] != $SpecData[$pd_v['preset_id']][0]['spec_value']) {
                    // 更新规格值数据
                    $UpData[] = [
                        'spec_id' => $SpecData[$pd_v['preset_id']][0]['spec_id'],
                        'spec_mark_id' => $pd_v['preset_mark_id'],
                        'spec_name' => $pd_v['preset_name'],
                        'spec_value_id' => $pd_v['preset_id'],
                        'spec_value' => $pd_v['preset_value'],
                        'update_time' => getTime(),
                    ];
                    // 删除产品已选规格表中对应需要更新的规格值数据
                    unset($SpecData[$pd_v['preset_id']]);
                } else {
                    // 删除规格库中不存在的规格值数据
                    unset($SpecData[$pd_v['preset_id']]);
                }
            }

            // 合并添加、编辑数据，统一处理
            $SaveData = array_merge($AddData, $UpData);

            /*处理需要删除的规格值数据*/
            if (!empty($SpecData)) {
                $DelIsSelect = 0;
                foreach ($SpecData as $key => $value) {
                    $SpecIds[] = $value[0]['spec_id'];
                    if (1 == $value[0]['spec_is_select']) {
                        $DelIsSelect = 1;
                    }
                    $spec_mark_id = $value[0]['spec_mark_id'];
                }
            }
            /* END */

            $HtmlTable = $SpecMarks = '';
            if (!empty($SpecIds)) {
                // 删除废弃的规格值数据
                Db::name('product_spec_data')->where('spec_id', 'IN', $SpecIds)->delete();
                if (1 == $DelIsSelect) {
                    session('spec_arr', null);
                    $SpecWhere = [
                        'aid' => $post['aid'],
                        'spec_is_select' => 1,// 已选中的
                    ];
                    $order = 'spec_value_id asc, spec_id asc';
                    $product_spec_data = Db::name('product_spec_data')->where($SpecWhere)->order($order)->select();
                    if (!empty($product_spec_data)) {
                        $spec_arr_new = group_same_key($product_spec_data, 'spec_mark_id');
                        $DelAllSpec = $spec_mark_id;
                        foreach ($spec_arr_new as $key => $value) {
                            $spec_mark_id_arr[] = $key;
                            for ($i = 0; $i < count($value); $i++) {
                                $spec_arr_new[$key][$i] = $value[$i]['spec_value_id'];
                            }
                            if ($spec_mark_id == $key) {
                                $DelAllSpec = 0;
                            }
                        }

                        session('spec_arr', $spec_arr_new);
                        $HtmlTable = $this->ProductSpecLogic->SpecAssemblyEdit($spec_arr_new, $post['aid']);
                        $SpecMarks = implode(',', $spec_mark_id_arr);
                    }
                }
            }

            if (!empty($SaveData)) {
                // 批量保存更新新规格
                model('ProductSpecData')->saveAll($SaveData);
            }

            if (!empty($UpSpecWhere) && !empty($UpSpecName)) {
                // 更新当前产品下对应的规格名称
                Db::name('product_spec_data')->where($UpSpecWhere)->update($UpSpecName);
                if (empty($UpData)) {
                    $UpData[0] = [
                        'spec_name' => $UpSpecName['spec_name'],
                        'spec_mark_id' => $UpSpecWhere['spec_mark_id']
                    ];
                }
            }

            $ValueOption = $this->ProductSpecLogic->GetPresetValueOption('', $post['spec_mark_id'], $post['aid']);
            $ResultData = [
                'UpData' => $UpData,
                'SpecIds' => $SpecIds,
                'HtmlTable' => $HtmlTable,
                'SpecMarks' => $SpecMarks,
                'DelAllSpec' => $DelAllSpec,
                'ValueOption' => $ValueOption,
            ];
            $this->success('同步成功，规格值已刷新！', null, $ResultData);
        }
    }

    // 新增产品时更新同步规格数据
    public function refresh_preset_value()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            if (!empty($post)) {
                $HtmlTable = $DelAllPreset = $PresetData = $MarkData = '';
                if ((isset($post['mark_mark_ids']) && !empty($post['mark_mark_ids'])) || (isset($post['mark_preset_ids']) && !empty($post['mark_preset_ids']))) {
                    if (!empty($post['mark_mark_ids'])) {
                        $MarkData = $this->product_spec_preset_db->where('preset_mark_id', 'IN', $post['mark_mark_ids'])->field('preset_mark_id, preset_name')->select();
                    }
                    if (!empty($post['mark_preset_ids'])) {
                        $PresetData = $this->product_spec_preset_db->where('preset_id', 'IN', $post['mark_preset_ids'])->field('preset_id, preset_value')->select();
                    }
                } else {
                    $DelAllPreset = 0;
                    $spec_arr_ses = session('spec_arr');
                    foreach ($spec_arr_ses[$post['preset_mark_id']] as $key => $value) {
                        if ($value == $post['preset_id']) {
                            unset($spec_arr_ses[$post['preset_mark_id']][$key]);
                        }
                    }
                    if (empty($spec_arr_ses[$post['preset_mark_id']])) {
                        unset($spec_arr_ses[$post['preset_mark_id']]);
                        $count = $this->product_spec_preset_db->where('preset_mark_id', $post['preset_mark_id'])->count();
                        if (empty($count)) {
                            $DelAllPreset = 1;
                        }
                    }
                    session('spec_arr', $spec_arr_ses);
                    $HtmlTable = $this->ProductSpecLogic->SpecAssembly($spec_arr_ses);
                }

                $ResultData = [
                    'MarkData' => $MarkData,
                    'HtmlTable' => $HtmlTable,
                    'PresetData' => $PresetData,
                    'DelAllPreset' => $DelAllPreset,
                ];
                $this->success('同步成功！', null, $ResultData);
            }
        }
    }

    // 检查是否最新的购物车标签
    public function VerifyLatestTemplate()
    {
        // 验证最新模板
        $ResultData = VerifyLatestTemplate();
        if (empty($ResultData)) {
            // 更新开启多规格
            getUsersConfigData('shop', ['shop_open_spec' => 1]);
            // 返回提示
            $this->success('模板检测通过，规格已开启！');
        } else {
            if (5 == count($ResultData)) {
                $msg = '未检测到规格标签，请根据提示手工调用后再重新验证！';
            } else {
                $msg = '规格标签缺少变量：<br/><span style="color: red;">' . implode('， ', $ResultData) . '</span><br/>请检查模板核实后再次验证！';
            }
            // 更新关闭多规格
            getUsersConfigData('shop', ['shop_open_spec' => 0]);
            // 返回提示
            $this->error($msg);
        }
    }

    // 暂时已废弃，暂时不清理删除代码
    public function FindSmptConfig()
    {
        $Smtp = tpCache('smtp');
        if (empty($Smtp['smtp_server']) || empty($Smtp['smtp_port']) || empty($Smtp['smtp_user']) || empty($Smtp['smtp_pwd']) || empty($Smtp['smtp_from_eamil'])) {
            $this->error('邮箱配置尚未配置完成，前往配置？', url('System/api_conf'));
        } else {
            // tpCache('smtp', [input('post.field') => 1]);
            $this->success('配置完成');
        }
    }

    // 未付款订单改价
    public function order_change_price()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            if (empty($post['order_id']) || empty($post['pay_use_cash'])) $this->error('操作错误，刷新重试');
            // 更新条件
            $where = [
                'order_id' => $post['order_id'],
                'order_status' => 0
            ];
            // 更新数据
            $Code = date('Ymd') . getTime() . rand(10, 100);
            $update = [
                'order_code' => $Code,
                'update_time' => getTime(),
                'pay_use_cash' => $post['pay_use_cash']
            ];
            // 更新操作
            $ResultID = $this->shop_order_db->where($where)->update($update);
            // 更新后续操作
            if (!empty($ResultID)) {
                // 添加订单的操作记录
                $action_note = '实付款更新为：' . $post['pay_use_cash'] . '，原实付款为：' . $post['pay_use_cash_old'];
                AddOrderAction($post['order_id'], 0, session('admin_id'), 0, 0, 0, '商家改价！', $action_note);
                $this->success('改价完成');
            } else {
                $this->error('改价失败，刷新重试');
            }
        }
    }

    //会员编辑 订单数列表
    public function users_edit_order_index()
    {
        // 初始化数组和条件
        $where = [
            'merchant_id' => 0,
        ];

        // 会员编辑专用 - 筛选
        $users_id = input('users_id/d');
        if (!empty($users_id)) {
            $where['a.users_id'] = $users_id;
            $where['a.order_status'] = 3;
        }
        // 订单号查询
        $order_code = input('order_code/s');
        if (!empty($order_code)) $where['a.order_code'] = ['LIKE', "%{$order_code}%"];

        // 分页查询
        $count = $this->shop_order_db->alias('a')->where($where)->count('order_id');
        $pageObj = new Page($count, config('paginate.list_rows'));

        // 订单主表数据查询
        $list = $this->shop_order_db->alias('a')
            ->field('a.*, b.username as u_username, b.nickname as u_nickname, b.mobile as u_mobile')
            ->where($where)
            ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
            ->order('a.order_id desc')
            ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
            ->select();
        if (empty($list) && !empty($order_code)) {
            // 通过商品名称查询订单号
            $where_1['product_name'] = ['LIKE', "%{$order_code}%"];
            $order_ids = $this->shop_order_details_db->where($where_1)->group('order_id')->column('order_id');
            // 重新查询订单主表
            unset($where['a.order_code']);
            $where['a.order_id'] = ['IN', $order_ids];
            // 分页查询
            $count = $this->shop_order_db->alias('a')->where($where)->count('order_id');
            $pageObj = new Page($count, config('paginate.list_rows'));
            // 订单主表数据查询
            $list = $this->shop_order_db->alias('a')
                ->field('a.*, b.username as u_username, b.nickname as u_nickname, b.mobile as u_mobile')
                ->where($where)
                ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
                ->order('a.order_id desc')
                ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
                ->select();
        }

        $order_ids = [];
        $OrderReminderID = [];
        foreach ($list as $key => $value) {
            array_push($order_ids, $value['order_id']);
            if (1 == $value['order_status']) array_push($OrderReminderID, $value['order_id']);
        }

        // 处理订单详情数据
        $where = [
            'a.order_id' => ['IN', $order_ids]
        ];
        $DetailsData = $this->shop_order_details_db->alias('a')
            ->field('a.*, b.service_id, b.status')
            ->where($where)
            ->join('__SHOP_ORDER_SERVICE__ b', 'a.details_id = b.details_id', 'LEFT')
            ->order('details_id asc')
            ->select();
        $ArchivesData = get_archives_data($DetailsData, 'product_id');
        foreach ($DetailsData as $key => $value) {
            // 商品链接
            $value['arcurl'] = get_arcurl($ArchivesData[$value['product_id']]);
            // 商品图片处理
            $value['litpic'] = handle_subdir_pic(get_default_pic($value['litpic']));
            // 售后信息处理
            $value['service_id'] = !empty($value['service_id']) ? $value['service_id'] : 0;
            $value['status'] = !empty($value['status']) ? $value['status'] : 0;
            $value['status_name'] = !empty($value['status']) && in_array($value['status'], [6, 7]) ? '维权完成' : '维权中';
            // 产品属性处理
            $goodsData = unserialize($value['data']);
            $value['product_attr'] = !empty($goodsData['product_attr']) ? $goodsData['product_attr'] : '';
            $value['product_spec'] = !empty($goodsData['product_spec']) ? $goodsData['product_spec'] : '';
            $value['product_spec'] = !empty($goodsData['product_spec']) ? "<span class='eyou_product_spec'>".str_replace("；", "</span><span class='eyou_product_spec'>", $goodsData['product_spec'])."</span>" : '';

            $DetailsData[$key] = $value;
        }

        // 把订单详情数据植入订单数据
        $defaultDetails = [
            'details_id' => 0,
            'order_id' => 0,
            'users_id' => 0,
            'product_id' => 0,
            'product_name' => '',
            'num' => 0,
            'data' => '',
            'product_price' => 0,
            'prom_type' => 0,
            'litpic' => get_default_pic(),
            'apply_service' => 0,
            'is_comment' => 0,
            'add_time' => 0,
            'update_time' => 0,
            'service_id' => 0,
            'status' => 0,
            'status_name' => '',
            'arcurl' => '',
        ];
        // 把订单详情数据植入订单数据
        $DetailsDataGroup = group_same_key($DetailsData, 'order_id');
        foreach ($list as $key => $value) {
            // 处理会员昵称
            $value['u_nickname'] = !empty($value['u_nickname']) ? $value['u_nickname'] : $value['u_username'];
            // 处理订单详情数据
            $value['Details'] = $DetailsDataGroup[$value['order_id']];
            if (empty($value['Details'])) $value['Details'] = [$defaultDetails];
            // 商品条数
            $value['rowspan'] = count($value['Details']);
            // 添加时间
            $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
            // 更新时间
            $value['update_time'] = date('Y-m-d H:i:s', $value['update_time']);
            // 处理订单主表的地址数据处理，显示中文名字
            $value['country'] = '中国';
            $value['province'] = get_province_name($value['province']);
            $value['city'] = get_city_name($value['city']);
            $value['district'] = get_area_name($value['district']);
            $value['all_address'] = "{$value['province']}-{$value['city']}-{$value['district']}-{$value['address']}";
            // 重新赋值数据
            $list[$key] = $value;
        }

        // 分页显示输出
        $pageStr = $pageObj->show();
        // 获取订单方式名称
        $pay_method_arr = Config::get('global.pay_method_arr');
        // 获取订单状态
        $admin_order_status_arr = Config::get('global.admin_order_status_arr');

        // 数据加载
        $this->assign('list', $list);
        $this->assign('page', $pageStr);
        $this->assign('pager', $pageObj);
        $this->assign('pay_method_arr', $pay_method_arr);
        $this->assign('admin_order_status_arr', $admin_order_status_arr);

        return $this->fetch('member/edit/order_index');
    }

    /**
     * 商家退货地址
     * @return [type] [description]
     */
    public function address_index()
    {
        // 初始化数组和条件
        $where = [];

        // 分页查询
        $count = Db::name('shop_return_address')->where($where)->count('id');
        $Page = new Page($count, config('paginate.list_rows'));
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('pager', $Page);

        // 订单主表数据查询
        $list = Db::name('shop_return_address')
            ->where($where)
            ->order('default desc')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        if (!empty($list)) {

            $province_arr = get_arr_column($list, 'province');
            $city_arr = get_arr_column($list, 'city');
            $area_arr = get_arr_column($list, 'area');
            $region_arr = array_merge($province_arr, $city_arr, $area_arr);
            $region = Db::name('region')->where('id', 'in', $region_arr)->field('id,name')->getAllWithIndex('id');
            foreach ($list as $k => $v) {
                $list[$k]['province'] = $region[$v['province']]['name'];
                $list[$k]['city'] = $region[$v['city']]['name'];
                $list[$k]['area'] = $region[$v['area']]['name'];
            }
        }

        // 数据加载
        $this->assign('list', $list);

        return $this->fetch();
    }

    /**
     * 新增商家地址
     * @return [type] [description]
     */
    public function address_add()
    {
        if (IS_POST) {
            $post = input('post.');
            if (empty($post['contact'])) $this->error('联系人不能为空');
            if (empty($post['mobile'])) $this->error('手机号不能为空');
            if (!check_mobile($post['mobile'])) $this->error('手机号格式不正确');
            if (empty($post['province']) || empty($post['city']) || empty($post['area'])) $this->error('所属地区不能为空');
            if (empty($post['address'])) $this->error('详细地址不能为空');
            // 添加新优惠券
            $res = Db::name('shop_return_address')->add($post);
            if (false !== $res) {
                $this->success('操作成功', url('Shop/address_index'));
            } else {
                $this->error('操作失败');
            }
        }
        $region['province_list'] = Db::name('region')->field('id, name')->where('level', 1)->select();
        $region['city_list'] = Db::name('region')->field('id, name')->where('level', 2)->where('parent_id', $region['province_list'][0]['id'])->select();
        $region['area_list'] = Db::name('region')->field('id, name')->where('level', 3)->where('parent_id', $region['city_list'][0]['id'])->select();

        $this->assign('region', $region);
        return $this->fetch();
    }

    public function get_region_list()
    {
        $level = input('param.level/d');
        $parent_id = input('param.id/d');
        $region = [];
        if (1 == $level) {
            $region['city_list'] = Db::name('region')->field('id, name')->where('level', 2)->where('parent_id', $parent_id)->select();
            $region['area_list'] = Db::name('region')->field('id, name')->where('level', 3)->where('parent_id', $region['city_list'][0]['id'])->select();
        } else if (2 == $level) {
            $region['area_list'] = Db::name('region')->field('id, name')->where('level', 3)->where('parent_id', $parent_id)->select();
        }
        $this->success('success', '', $region);
    }

    /**
     * 编辑商家地址
     * @return [type] [description]
     */
    public function address_edit()
    {
        if (IS_POST) {
            $post = input('post.');
            if (empty($post['contact'])) $this->error('联系人不能为空');
            if (empty($post['mobile'])) $this->error('手机号不能为空');
            if (!check_mobile($post['mobile'])) $this->error('手机号格式不正确');
            if (empty($post['province']) || empty($post['city']) || empty($post['area'])) $this->error('所属地区不能为空');
            if (empty($post['address'])) $this->error('详细地址不能为空');
            $post['update_time'] = getTime();
            $id = $post['id'];
            unset($post['id']);
            $res = Db::name('shop_return_address')->where('id', $id)->update($post);
            if (false !== $res) {
                $this->success('操作成功', url('Shop/address_index'));
            } else {
                $this->error('操作失败');
            }
        }
        $id = input('param.id/d');
        $info = Db::name('shop_return_address')->where('id', $id)->find();
        $region['province_list'] = Db::name('region')->field('id, name')->where('level', 1)->select();
        $region['city_list'] = Db::name('region')->field('id, name')->where('level', 2)->where('parent_id', $info['province'])->select();
        $region['area_list'] = Db::name('region')->field('id, name')->where('level', 3)->where('parent_id', $info['city'])->select();

        $this->assign('info', $info);
        $this->assign('region', $region);
        return $this->fetch();
    }

    /**
     * 删除商家地址
     * @return [type] [description]
     */
    public function address_del()
    {
        $id = input('del_id/a');
        $id = eyIntval($id);
        if (IS_AJAX_POST && !empty($id)) {
            // 删除统一条件
            $where['id'] = ['IN', $id];
            $where['default'] = ['neq', 1];
            $title = Db::name('shop_return_address')->where($where)->column('title');
            $return = Db::name('shop_return_address')->where($where)->delete();
            if ($return) {
                adminLog("删除商家退货地址：" . implode(',', $title));
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
        $this->error('参数有误');
    }

    //设置默认地址
    // public function address_set_default()
    // {
    //     $id = input('param.id/d');
    //     Db::name('shop_return_address')->where('id', 'neq', $id)->update(['default' => 0, 'update_time' => getTime()]);
    //     $res = Db::name('shop_return_address')->where('id', $id)->update(['default' => 1, 'update_time' => getTime()]);
    //     if (false !== $res) {
    //         $this->success('设置默地址成功');
    //     } else {
    //         $this->error('设置默地址失败');
    //     }
    // }
}